package code;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import data.Data;
import file.File2DataList;

public class TenFoldCrossValidation_MakeDataset {

	/**
	 * @param args
	 */
	
	public static void main(String[] args) throws IOException {
		
		String dir = "C:\\Users\\mtnguyen.2012\\Downloads\\ml-1m\\ml-1m\\";
		String ratFile = "ratings.dat";
		
		File2DataList f2dl = new File2DataList(dir + ratFile, "::");
		List<Data> data = f2dl.getList();

		BufferedWriter train1 = new BufferedWriter(new FileWriter("D:\\MovieLens\\train1"));
		BufferedWriter test1 = new BufferedWriter(new FileWriter("D:\\MovieLens\\test1"));

		BufferedWriter train2 = new BufferedWriter(new FileWriter("D:\\MovieLens\\train2"));
		BufferedWriter test2 = new BufferedWriter(new FileWriter("D:\\MovieLens\\test2"));

		BufferedWriter train3= new BufferedWriter(new FileWriter("D:\\MovieLens\\train3"));
		BufferedWriter test3 = new BufferedWriter(new FileWriter("D:\\MovieLens\\test3"));

		BufferedWriter train4 = new BufferedWriter(new FileWriter("D:\\MovieLens\\train4"));
		BufferedWriter test4 = new BufferedWriter(new FileWriter("D:\\MovieLens\\test4"));

		BufferedWriter train5 = new BufferedWriter(new FileWriter("D:\\MovieLens\\train5"));
		BufferedWriter test5 = new BufferedWriter(new FileWriter("D:\\MovieLens\\test5"));

		/*BufferedReader trainingSet = new BufferedReader(new FileReader("D:\\Netflix Dataset\\download\\training_set\\smalltrain"));
		BufferedReader testSet = new BufferedReader(new FileReader("D:\\Netflix Dataset\\download\\training_set\\smallvalidate"));

		BufferedWriter train1 = new BufferedWriter(new FileWriter("D:\\Netflix Dataset\\download\\train1"));
		BufferedWriter test1 = new BufferedWriter(new FileWriter("D:\\Netflix Dataset\\download\\test1"));

		BufferedWriter train2 = new BufferedWriter(new FileWriter("D:\\Netflix Dataset\\download\\train2"));
		BufferedWriter test2 = new BufferedWriter(new FileWriter("D:\\Netflix Dataset\\download\\test2"));

		BufferedWriter train3= new BufferedWriter(new FileWriter("D:\\Netflix Dataset\\download\\train3"));
		BufferedWriter test3 = new BufferedWriter(new FileWriter("D:\\Netflix Dataset\\download\\test3"));

		BufferedWriter train4 = new BufferedWriter(new FileWriter("D:\\Netflix Dataset\\download\\train4"));
		BufferedWriter test4 = new BufferedWriter(new FileWriter("D:\\Netflix Dataset\\download\\test4"));

		BufferedWriter train5 = new BufferedWriter(new FileWriter("D:\\Netflix Dataset\\download\\train5"));
		BufferedWriter test5 = new BufferedWriter(new FileWriter("D:\\Netflix Dataset\\download\\test5"));
		
		String line;
		int i = 0;
		List<String> data = new ArrayList<String>();
		
		while((line = trainingSet.readLine()) != null){
			data.add(line);
		}
		
		while((line = testSet.readLine()) != null){
			data.add(line);
		}*/
		
		int i=0;
		
		for(Data datum : data){
			int uid = datum.uid;
			short iid = datum.iid;
			byte rat = datum.rat;

			switch(i){
				case 0:
					test1.write(uid+","+iid+","+rat+"\n");
					train2.write(uid+","+iid+","+rat+"\n");
					train3.write(uid+","+iid+","+rat+"\n");
					train4.write(uid+","+iid+","+rat+"\n");
					train5.write(uid+","+iid+","+rat+"\n");
					i=1;
					break;
				case 1:
					test2.write(uid+","+iid+","+rat+"\n");
					train1.write(uid+","+iid+","+rat+"\n");
					train3.write(uid+","+iid+","+rat+"\n");
					train4.write(uid+","+iid+","+rat+"\n");
					train5.write(uid+","+iid+","+rat+"\n");
					i=2;
					break;
				case 2:
					test3.write(uid+","+iid+","+rat+"\n");
					train2.write(uid+","+iid+","+rat+"\n");
					train1.write(uid+","+iid+","+rat+"\n");
					train4.write(uid+","+iid+","+rat+"\n");
					train5.write(uid+","+iid+","+rat+"\n");
					i=3;
					break;
				case 3:
					test4.write(uid+","+iid+","+rat+"\n");
					train2.write(uid+","+iid+","+rat+"\n");
					train3.write(uid+","+iid+","+rat+"\n");
					train1.write(uid+","+iid+","+rat+"\n");
					train5.write(uid+","+iid+","+rat+"\n");
					i=4;
					break;
				case 4:
					test5.write(uid+","+iid+","+rat+"\n");
					train2.write(uid+","+iid+","+rat+"\n");
					train3.write(uid+","+iid+","+rat+"\n");
					train4.write(uid+","+iid+","+rat+"\n");
					train1.write(uid+","+iid+","+rat+"\n");
					i=0;
					break;
			}
		}
		
//		trainingSet.close();
//		testSet.close();
		
		train1.close();
		test1.close();
		
		train2.close();
		test2.close();
		
		train3.close();
		test3.close();
		
		train4.close();
		test4.close();
		
		train5.close();
		test5.close();
	}

}
